package boot.spring.service;

import boot.spring.entity.SocketUserEntity;
import boot.spring.mapper.SocketUserMapper;
import boot.spring.utils.DESUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author pan.shun
 * @since 2022/6/15 14:30
 */
@Slf4j
@Service
public class SocketUserService extends ServiceImpl<SocketUserMapper, SocketUserEntity> {

    public boolean doCheckUser(String userName, String password) {
        try {
            LambdaQueryWrapper<SocketUserEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SocketUserEntity::getPassword, DESUtils.Encrypt(password));
            queryWrapper.eq(SocketUserEntity::getUserName, userName);
            int count = this.count(queryWrapper);
            return count != 0;
        } catch (Exception e) {
            log.error("登录发生了异常，异常信息：{}", e);
            return false;
        }
    }

    public String getUidByName(String userName, String password) {
        LambdaQueryWrapper<SocketUserEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SocketUserEntity::getPassword, DESUtils.Encrypt(password));
        queryWrapper.eq(SocketUserEntity::getUserName, userName);
        SocketUserEntity socketUserEntity = this.getOne(queryWrapper);
        return socketUserEntity.getId();
    }
}
